//
//  GYLoginView.m
//  Visitor
//
//  Created by x on 2017/9/11.
//  Copyright © 2017年 HLB. All rights reserved.
//

#import "GYLoginView.h"
#import "UIImageView+GYExtension.h"
#import "UITextField+XMGTextField.h"
#import "UIButton+GYButton.h"
#import "UILabel+GYLable.h"
#import "UITextField+XMGTextField.h"

@interface GYLoginView ()<UITextFieldDelegate>

/** 用户头像 */
@property (nonatomic, weak) UIImageView *iconHeadImage;

/** 用户输入框图片 */
@property (nonatomic, weak) UIImageView *userImage;

/** 密码输入框的图片 */
@property (nonatomic, weak) UIImageView *pwdImage;

/** 用户名的标记线 */
@property (nonatomic, weak) UILabel *userMarkLable;

/** 密码标记的标记线 */
@property (nonatomic, weak) UILabel *pwdmarkLable;

/** 用户名输入textfiled */
@property (nonatomic, weak) UITextField *userTextFiled;

/** 密码输入textfiled */
@property (nonatomic, weak) UITextField *pwdTextFiled;

@end

@implementation GYLoginView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        //初始化
        [self setupUI];
        
    }
    return self;
}

/**
 初始化控件
 */
- (void)setupUI {
    //1.添加子控件
    UIImageView *iconHeadImage = [UIImageView imageView:@"enter_icon_id_sel"];
    self.iconHeadImage = iconHeadImage;
    [self addSubview:self.iconHeadImage];
    
    UIImageView *userImage = [UIImageView imageView:@"enter_icon_id_nor"];
    self.userImage = userImage;
    [self addSubview:userImage];
    
    UIImageView *pwdImage = [UIImageView imageView:@"enter_icon_code_nor"];
    self.pwdImage = pwdImage;
    [self addSubview:pwdImage];
    
    UILabel *markLable = [UILabel lableWithBackGround];
    self.userMarkLable = markLable;
    [self addSubview:markLable];
    
    UILabel *markLable1 = [UILabel lableWithBackGround];
    self.pwdmarkLable = markLable1;
    [self addSubview:markLable1];
    
    //创建输入用户名textfiled
    UITextField *userTextFiled = [UITextField textFiled:RGBColor(0X3A9DF1) placeholder:@"请输入用户名" tag:1 target:self action:@selector(textFileReturnClick:)];
    self.userTextFiled = userTextFiled;
    [self addSubview:userTextFiled];
    
    //创建输入密码textfiled
    UITextField *pwdTextFiled = [UITextField textFiled:RGBColor(0X3A9DF1) placeholder:@"请输入用密码" tag:2 target:self action:@selector(textFileReturnClick:)];
    pwdTextFiled.secureTextEntry = YES;//设置成密码格式
    self.pwdTextFiled = pwdTextFiled;
    [self addSubview:pwdTextFiled];
    
    //创建一个登入按钮
    UIButton *loginBtu = [UIButton buttonWith:@"登入" titleColor:RGBColor(0XFFFFFF) borderColor:RGBColor(0X3A9DF1) backgroundColor:RGBColor(0X3A9DF1) tag:1 target:self action:@selector(btuClick:)];
    [self addSubview:loginBtu];
    
//    //创建一个注册按钮
//    UIButton *registerBtu = [UIButton buttonWith:@"注册" titleColor:RGBColor(0X3A9DF1) borderColor:RGBColor(0X3A9DF1) backgroundColor:[UIColor whiteColor] tag:2 target:self action:@selector(btuClick:)];
//    [self addSubview:registerBtu];
//
//    //创建一个找回密码按钮
//    UIButton *pwdBtu = [UIButton buttonWithTitle:@"找回密码" titleColor:RGBColor(0X3A9DF1) tag:3 target:self action:@selector(btuClick:)];
//    [self addSubview:pwdBtu];
    //2.布局子控件
    NSDictionary *dic = @{@"iconHeadImage":iconHeadImage,
                          @"userImage":userImage,
                          @"pwdImage":pwdImage,
                          @"markLable":markLable,
                          @"markLable1":markLable1,
                          @"loginBtu":loginBtu,
                          //@"registerBtu":registerBtu,
                          //@"pwdBtu":pwdBtu,
                          @"userTextFiled":userTextFiled,
                          @"pwdTextFiled":pwdTextFiled
                          };
    NSDictionary *metrics = @{@"margin":@((WIDTH-84)/2),
                              @"width":@(WIDTH-60),
                              @"pwdMargin":@((WIDTH-80)/2),
                              @"textFiledW":@(WIDTH-120)
                              };
    
    //头像
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-margin-[iconHeadImage(84)]" options:0 metrics:metrics views:dic]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-54-[iconHeadImage(84)]" options:0 metrics:nil views:dic]];
    
    //输入用户名
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-42-[userImage(22)]" options:0 metrics:nil views:dic]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-200-[userImage(22)]" options:0 metrics:nil views:dic]];
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[userImage]-18-[userTextFiled(textFiledW)]" options:0 metrics:metrics views:dic]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[userTextFiled(30)]-0-[markLable]" options:0 metrics:nil views:dic]];
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-30-[markLable(width)]" options:0 metrics:metrics views:dic]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[userImage]-4-[markLable(1)]" options:0 metrics:nil views:dic]];
    
    //输入用户密码
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-42-[pwdImage(22)]" options:0 metrics:nil views:dic]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[userImage]-54-[pwdImage(22)]" options:0 metrics:nil views:dic]];
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[pwdImage]-18-[pwdTextFiled(textFiledW)]" options:0 metrics:metrics views:dic]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[pwdTextFiled(30)]-0-[markLable1]" options:0 metrics:nil views:dic]];
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-30-[markLable1(width)]" options:0 metrics:metrics views:dic]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[pwdImage]-4-[markLable1(1)]" options:0 metrics:nil views:dic]];

    //布局按钮
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-30-[loginBtu(width)]" options:0 metrics:metrics views:dic]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[markLable1]-36-[loginBtu(44)]" options:0 metrics:nil views:dic]];
    
//    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-30-[registerBtu(width)]" options:0 metrics:metrics views:dic]];
//    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[loginBtu]-24-[registerBtu(44)]" options:0 metrics:nil views:dic]];

    
//    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-pwdMargin-[pwdBtu]-pwdMargin-|" options:0 metrics:metrics views:dic]];
//    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[pwdBtu(30)]-20-|" options:0 metrics:nil views:dic]];
}

#pragma mark -- 按钮点击事件

- (void)btuClick:(UIButton *)btu {
    switch (btu.tag) {
        case 1:
        {
            //1.判断用户名和密码是否符合规则
            //if (![self.userTextFiled.text isEqualToString:@""] && self.userTextFiled.text != nil && ![self.pwdTextFiled.text isEqualToString:@""] && self.pwdTextFiled.text != nil) {
                if ([self.delegate respondsToSelector:@selector(loginBtuClick:pwd:)]) {
                    [self.delegate loginBtuClick:self.userTextFiled.text pwd:self.pwdTextFiled.text];
                }
//            }else {
//                [SVProgressHUD showImage:nil status:@"用户名/密码不正确!"];
//            }
        }
            break;
            
        case 2:
        {
            if ([self.delegate respondsToSelector:@selector(registerBtuClick)]) {
                [self.delegate registerBtuClick];
            }
        }
            break;
            
        case 3:
        {
            if ([self.delegate respondsToSelector:@selector(pwdRetrieval)]) {
                [self.delegate pwdRetrieval];
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark -- textFiled的事件

- (void)textFileReturnClick:(UITextField *)textFiled {
    [textFiled resignFirstResponder];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    //开始编辑
    if (textField.tag == 1) {
        self.userImage.image = [UIImage imageNamed:@"enter_icon_id_sel"];
        self.userMarkLable.backgroundColor = RGBColor(0X3A9DF1);
    }else {
        self.pwdImage.image = [UIImage imageNamed:@"enter_icon_code_sel"];
        self.pwdmarkLable.backgroundColor = RGBColor(0X3A9DF1);//RGBColor(0XCCCCCC);
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    //结束编辑
    if (textField.tag == 1) {
        self.userImage.image = [UIImage imageNamed:@"enter_icon_id_nor"];
        self.userMarkLable.backgroundColor = RGBColor(0XCCCCCC);
    }else {
        self.pwdImage.image = [UIImage imageNamed:@"enter_icon_code_nor"];
        self.pwdmarkLable.backgroundColor = RGBColor(0XCCCCCC);
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.userTextFiled resignFirstResponder];
    [self.pwdTextFiled resignFirstResponder];
}
@end
